from werkzeug.security import generate_password_hash, check_password_hash

from ..exts import db
from . import BaseModel


class CinemaAddress(BaseModel):  # models/cinema_admin/cinema_address_model.py
    name = db.Column(db.String(64), comment='电影院名称')
    city = db.Column(db.String(16), comment='电影院所在城市')
    district = db.Column(db.String(16), comment='城市区域')
    address = db.Column(db.String(128), comment='详细地址')
    phone = db.Column(db.String(32), comment='电影院电话')
    score = db.Column(db.Float, default=10, comment='电影院评分')
    hall_num = db.Column(db.Integer, default=1, comment='大厅数量')
    serve_score = db.Column(db.Float, default=10, comment='服务评分')
    as_trict = db.Column(db.Float, default=10, comment='严格程度')
    flag = db.Column(db.Boolean, default=False, comment='电影院标记')
    is_delete = db.Column(db.Boolean, default=False, comment='是否有')

    cinema_user_id = db.Column(db.Integer, db.ForeignKey('tb_cinema_user.id'))


class Hall(BaseModel):  # models/cinema_admin/cinema_hall_model
    __tablename__ = 'tb_hall'
    address_id = db.Column(db.Integer, db.ForeignKey(CinemaAddress.id))
    num = db.Column(db.Integer, default=1, comment='大厅编号')
    seats = db.Column(db.String(128), comment='座位规格')


class HallMovie(BaseModel):  # models/cinema_admin/cinema_hall_movie_model
    __tablename__ = 'tb_hall_movie'
    movie_id = db.Column(db.Integer, db.ForeignKey('tb_movies.id'))
    hall_id = db.Column(db.Integer, db.ForeignKey(Hall.id))
    time = db.Column(db.DateTime, comment='时间')
    # orders = db.relationship('MovieOrder', backref='HallMovie', lazy=True)


class CinemaMovie(BaseModel): # models/cinema_admin/cinema_movie_model
    __tablename__ = 'third_cinema_user_movie'
    cinema_user_id = db.Column(db.Integer, db.ForeignKey('tb_cinema_user.id'))
    movie_id = db.Column(db.Integer, db.ForeignKey('tb_movies.id'))



class CinemaUser(BaseModel):  # models/cinema_admin/cinema_user_model
    __tablename__ = 'tb_cinema_user'
    username = db.Column(db.String(32), unique=True, comment='用户名')
    _password = db.Column(db.String(256), comment='密码')
    phone = db.Column(db.String(32), unique=True, comment='手机号码')
    is_delete = db.Column(db.Boolean, default=False, comment='是否删除')
    is_verify = db.Column(db.Boolean, default=False, comment='权限认证')

    @property
    def password(self):
        raise Exception("Access Denied")

    @password.setter
    def password(self, pwd):
        self._password = generate_password_hash(pwd)

    def check_password(self, pwd):
        return check_password_hash(self._password, pwd)

    def check_permission(self, auth):
        if not self.is_verify:
            return False

        auths = CinemaUserAuth.query.filter_by(c_user_id=self.id)
        for au in auths:
            if auth == Auths.query.get(au.cinema_auth_id).auth_name:
                return True

        return False


class Auths(BaseModel):  # models/cinema_admin/cinema_user_model
    __tablename__ = 'tb_auths'
    auth_name = db.Column(db.String(64), unique=True, comment='权限名')


class CinemaUserAuth(BaseModel):  # models/cinema_admin/cinema_user_model
    __tablename__ = 'third_cinema_user_auth'
    cinema_user_id = db.Column(db.Integer, db.ForeignKey(CinemaUser.id))
    cinema_auth_id = db.Column(db.Integer, db.ForeignKey(Auths.id))

